package com.edu.dao;

import java.sql.*;

public class BaseDao {
    public static final String URL="jdbc:mysql://127.0.0.1:3306/demo?";
    public static final String USER="root";
    public static final String PASS="123456";
    public static final String DRIVER="com.mysql.cj.jdbc.Driver";

    protected Connection conn=null;
    protected PreparedStatement ps = null;
    /**
     * 获得数据库连接
     * */
    public static Connection getConn() {
        try {
            Class.forName(DRIVER);
            Connection conn= DriverManager.getConnection(URL, USER, PASS);
            if(conn != null) {
                System.out.println("连接成功");
            }else {
                System.out.println("数据库连接失败");
            }
            return conn;
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }
    /**
     * 释放资源Connection，PreparedStatement，ResultSet
     * */
    public static void closeAll(Connection conn, Statement ps, ResultSet rs) {
        try {
            if(rs !=null) {rs.close();}
            if(ps !=null) {ps.close();}
            if(conn !=null) {conn.close();}
        } catch (SQLException e) {e.printStackTrace();}
    }

    /**
     * 释放资源Connection，PreparedStatement
     * */
    public static void closeAll(Connection conn,PreparedStatement ps) {
        try {
            if(ps !=null) {ps.close();}
            if(conn !=null) {conn.close();}
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


}
